package com.cwvs.cat.adapter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


import com.cwvs.cat.FoundActiveDetailActivity;
import com.cwvs.cat.R;
import com.cwvs.cat.bean.ActiveContent;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class FoundActiveListAdapter extends BaseAdapter {

	private Activity mContent;
	private LayoutInflater inflater;
	private List<ActiveContent> list_content;

	public FoundActiveListAdapter() {
		// TODO Auto-generated constructor stub
	}

	public FoundActiveListAdapter(Activity ac, List<ActiveContent> list) {
		// TODO Auto-generated constructor stub
		this.mContent = ac;
		inflater = LayoutInflater.from(ac);
		this.list_content = list;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list_content.size();
	}

	@Override
	public Object getItem(int postion) {
		// TODO Auto-generated method stub
		return list_content.get(postion);
	}

	@Override
	public long getItemId(int postion) {
		// TODO Auto-generated method stub
		return postion;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewHolder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_list_found_active,
					null);
			viewHolder = new ViewHolder();
			viewHolder.txt_found_active_name = (TextView) convertView.findViewById(R.id.txt_found_active_name);
			viewHolder.time = (TextView) convertView.findViewById(R.id.time);
			viewHolder.school = (TextView) convertView.findViewById(R.id.school);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		convertView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent=new Intent();
				intent.setClass(mContent, FoundActiveDetailActivity.class);
				intent.putExtra("act_id", list_content.get(position).id);
				mContent.startActivity(intent);
			}
		});
		String time=list_content.get(position).begin_time;  
		Date date=null;  
		SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
		try {
			date=formatter.parse(time);
			SimpleDateFormat formatter2 =new SimpleDateFormat("MM-dd HH:mm:ss");  
			time=formatter2.format(date);  
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
		viewHolder.txt_found_active_name.setText(list_content.get(position).title);
		viewHolder.time.setText(time);
		viewHolder.school.setText(list_content.get(position).location);
		return convertView;
	}

	class ViewHolder {
		TextView txt_found_active_name, time,
		school;
	}

}